﻿using Cxuu.Common;
using Cxuu.Models;
using Cxuu.Service;
using Cxuu.Web.Authorize;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace Cxuu.Web.Admin;

[Area("Admin")]
[Authorize]
public class ArticleMarkController : Base
{
    private readonly LoginUtil _LoginUtil;
    private readonly ArticleMarkService _articleMark;

    public ArticleMarkController(
        LoginUtil LoginUtil,
        ArticleMarkService articleMark
        )
    {
        _LoginUtil = LoginUtil;
        _articleMark = articleMark;
    }

    [HttpGet]
    //[ActionPermissionFilter(Permission = PermissionKeys.Article_Mark)]
    public IActionResult ArticleMark()
    {
        return View();
    }

    [HttpGet]
    //[ActionPermissionFilter(Permission = PermissionKeys.Article_Mark)]
    public IActionResult MarkPreview()
    {
        return View();
    }

    [HttpGet]
    // [ActionPermissionFilter(Permission = PermissionKeys.Article_Mark)]

    public async Task<IActionResult> GetArticleMark(int id)
    {
        ViewArticleMark articleMark = await _articleMark.GetOneAsync(id);
        return Json(new { status = 1, data = articleMark });
    }

    [HttpPost]
    //  [ActionPermissionFilter(Permission = PermissionKeys.Article_Mark)]
    [Log(Name = "标记报送文件")]
    public async Task<IActionResult> InsertOrUpdateAsync(EditArticleMark articleMark)
    {
        if (ModelState.IsValid)
        {
            UserInfo adminUser = await _LoginUtil.UserInfoAsync();

            if (adminUser.PostCode != AdminPostConst.MASTER && adminUser.PostCode != AdminPostConst.DEPUTY)
                return Json(new { status = 0, msg = "必须部门 负责人(master) 或 副职(deputy) 才能标记" });

            articleMark.Uid = adminUser.UserId;
            var res = await _articleMark.InsertOrUpdateAsync(articleMark);
            if (res > 0)
                return Json(new { status = 1, msg = "标记文件 ID：" + articleMark.Aid });
            else
                return Json(new { status = 0, msg = "标记文件 ID：" + articleMark.Aid });
        }
        else
        {
            var error = ModelState.GetErrorMessages();
            return Json(new { status = 0, msg = error });
        }
    }

    [HttpPost]
    [Log(Name = "删除信息报送标记")]
    public async Task<IActionResult> Delete(int id)
    {
        UserInfo adminUser = await _LoginUtil.UserInfoAsync();
        if (adminUser.PostCode != AdminPostConst.MASTER && adminUser.PostCode != AdminPostConst.DEPUTY)
            return Json(new { status = 0, msg = "必须部门 负责人(master) 或 副职(deputy) 才能删除标记" });

        if (await _articleMark.DeleteByPriKeyAsync(id) > 0)
        {
            return Json(new { status = 1, msg = "删除信息报送的标记 ID：" + id });
        }
        else
            return Json(new { status = 0, msg = "删除失败！" });
    }
}


